﻿using System;
using System.ComponentModel;

using Caliburn.Micro;

using Microsoft.Practices.Unity;

using TheKitchen.Client.Commanding;
using TheKitchen.Client.Services;

namespace TheKitchen.Client.ViewModels
{
    public class NewRecipeViewModel : PropertyChangedBase, IDataErrorInfo, IActivate, IViewAware
    {
        #region Private Definitions

        private readonly ValidationHandler validationHandler = new ValidationHandler();
        private string name;
        private string description;
        private IModalView modalView;

        [Dependency]
        public ICommandAgent CommandAgent { get; set; }

        #endregion

        public string Name
        {
            get { return name; }
            set
            {
                validationHandler.ValidateRule("Name", "Required", "Is required", () => !string.IsNullOrEmpty(value));
                validationHandler.ValidateRule("Name", "Length", "Should not exceed 60 characters",
                    () => value.Length <= 60);
                
                name = value;

                NotifyOfPropertyChange(() => Name);
                NotifyOfPropertyChange(() => CanSave);
            }
        }

        public string Description
        {
            get { return description; }
            set
            {
                validationHandler.ValidateRule("Description", "Required", "Is required",
                    () => !string.IsNullOrEmpty(value));
                
                description = value;

                NotifyOfPropertyChange(() => Description);
                NotifyOfPropertyChange(() => CanSave);
            }
        }

        public bool IsActive
        {
            get { return true; }
        }

        /// <summary>
        ///   Gets a message that describes any validation errors for the object.
        /// </summary>
        /// <value></value>
        /// <returns>The validation error on the object, or null or <see cref = "F:System.String.Empty" /> if there are no errors present. </returns>
        public string Error
        {
            get { return null; }
        }

        public void Activate()
        {
            Name = "";
            Description = "";
        }

        public event EventHandler<ActivationEventArgs> Activated;

        public void Save()
        {
            bool isValid = !validationHandler.HasBrokenRules;
            if (isValid)
            {
                CommandAgent.Send(new AddNewRecipeCommand
                {
                    Id = Guid.NewGuid(),
                    Name = Name,
                    Description = Description
                });

                modalView.Close();
            }
        }

        public bool CanSave
        {
            get
            {
                return !validationHandler.HasBrokenRules;
            }
        }

        public void Cancel()
        {
            modalView.Close();
        }

        /// <summary>
        /// Gets the error message for a particular property if any.
        /// </summary>
        public string this[string propertyName]
        {
            get { return validationHandler[propertyName]; }
        }

        public void AttachView(object view, object context)
        {
            modalView = (IModalView) view;
        }

        public object GetView(object context)
        {
            return null;
        }

        public event EventHandler<ViewAttachedEventArgs> ViewAttached;
    }

    public interface IModalView
    {
        void Close();
    }
}